package com.itheima.ydd.common;


import com.itheima.ydd.entity.SysUser;
import com.itheima.ydd.entity.YddUser;

/**
 * 工具类：实现向threadlocal存储数据的方法
 */
public class UserHolder {

    private static ThreadLocal<Object> tl = new ThreadLocal<>();


    //将用户对象，存入Threadlocal
    public static void set(Object user) {
        tl.set(user);
    }

    //从当前线程，获取用户对象
    public static Object get() {
        return tl.get();
    }
    //从当前线程，获取用户对象的id
    public static String getUserId() {
        //拿到当前线程对象
        Object user=tl.get();
        //根据对象类型 返回相应对象的Id
        if(user instanceof YddUser){
            YddUser yddUser =(YddUser) user;
            return yddUser.getId().toString();
        }
        if(user instanceof SysUser){
            SysUser sysUser =(SysUser) user;
            return sysUser.getId();
        }
       return null;
    }



    //清空
    public static void remove() {
        tl.remove();
    }
}
